import java.applet.*;
import java.awt.*;

public class SailorApplet extends Applet implements Runnable {

   Thread t = null;
   boolean threadSuspended;
	Sailor[] theSailors;
	int width, height;
	int boat;			//Store total amount of sailors that reached boat
	int courtmartialed;	//Store total amount of sailors court martialed
	int remain;			//Store total amount of sailors to remain in whiskey
	int sailor;			//Sailor selection variable
	Font font = new Font("Arial",Font.BOLD,30);
    Color dockcolor;	//Store dock colour
    Color boatcolor;	//Store boat colour
   

   // Executed when the applet is first created.
   public void init() {
	   width = getSize().width;				//Gets width of applet in browser window
	   height = getSize().height;			//Gets height of applet in browser window

      setBackground( Color.BLUE );			//Sets background...(sea)
      sailor = 0;							//This sets the program to start with sailor at position 0 of array
      
      //Colours
      dockcolor = new Color(210,105,30);	//Sets dock colour
      boatcolor = new Color(165,42,42);		//Set boat colour
      
      
      //Sailor Stuff
      	theSailors = new Sailor[50];		//Sets the amount of sailors to 50
      			//Variables to store result of sailors
		boat = 0;							//Initialises boats variable
		courtmartialed = 0;					//Initialises courtmartialed variable
		remain = 0;							//Initialises remain variable
		
		
      while(sailor <= 49){					//Initialies the sailors
    	theSailors[sailor] = new Sailor();
    	sailor++;
      }
      sailor = 0;							//Resets back to first sailor
   }

   // Executed after the applet is created; and also whenever
   // the browser returns to the page containing the applet.
   public void start() {
      if ( t == null ) {
         t = new Thread( this );
         threadSuspended = false;
         t.start();
      }
      else {
         if ( threadSuspended ) {
            threadSuspended = false;
            synchronized( this ) {
               notify();
            }
         }
      }
   }

   // Executed whenever the browser leaves the page containing the applet.

   // Executed within the thread that this applet created.
   public void run() {
      try {
         while (true) {
           t.sleep(500);  	//Interval between steps // CHANGE BACK TO 500
           repaint();		//Re-paints
         }
      }
      catch (InterruptedException e) { }
   }

   
   // Executed whenever the applet is asked to redraw itself.
   public void paint( Graphics g ) {
	  
	   
	      //Paints Whiskey
	      g.setColor(Color.lightGray);
	      g.fillRect(0, 0, width, 25);
	      
	      //Paints Dock
	      g.setColor(dockcolor);
	      g.fillRect(187, 25, 125, 520);
	      
	      //Paints Boat
	      g.setColor(boatcolor);
	      g.fillRect(24, 545, 400, height - 555);
	  
	  
	  
	
	  //Sailors
	  g.setColor( Color.green );	//Sets Colour
	  
	  		//Paints Sailor
      g.fillOval( (theSailors[sailor].getPosx() + 10) * 25, (theSailors[sailor].getPosy() + 1) * 25, 10, 10 );
      
      //Writing
      
      g.setFont(font);
      g.drawString("Sailor number " + (sailor + 1), 500, 50);
      g.drawString("Sailors on boat: " + boat,500,425);
      g.drawString("Sailors court martialed: " + courtmartialed,500,475);
      g.drawString("Sailors stuck in Whiskey: " + remain,500,525);
      
      g.setColor(Color.white);
      g.drawString("Whiskey", 1100, 25);		//Labels Whiskey
      g.setColor(Color.white);
      g.drawString("Boat", 100, height - 50);	//Labels Boat

      
      //If sailor off dock, add final position to result variables
	  if(theSailors[sailor].onDock() == false && (boat + courtmartialed + remain) < 50){
		  
		  if(theSailors[sailor].getFinalPosition() == 1){
			  boat++;
		  }
		  else if(theSailors[sailor].getFinalPosition() == 2){
			  courtmartialed++;
		  }
		  else if(theSailors[sailor].getFinalPosition() == 3){
			  remain++;
		  }
		  
		  try {
			Thread.sleep(1000);			//Extra pause to see that sailor is off dock
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		if(sailor < theSailors.length - 1){
		sailor++;	//If sailor off dock, get next sailor
		}
	  }
	  else if (sailor < theSailors.length){
      theSailors[sailor].update();		//Updates Sailor's movement
	  }
      
   }
   
}
